package com.library.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.Result;
import com.library.entity.Book;
import com.library.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书管理控制器
 */
@Tag(name = "图书管理")
@RestController
@RequestMapping("/books")
@RequiredArgsConstructor
public class BookController {
    
    private final BookService bookService;
    
    @Operation(summary = "分页查询图书")
    @GetMapping
    public Result<Page<Book>> pageBooks(@RequestParam(defaultValue = "1") Integer current,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        @RequestParam(required = false) String title,
                                        @RequestParam(required = false) String author,
                                        @RequestParam(required = false) Long categoryId) {
        Page<Book> page = bookService.pageBooks(current, size, title, author, categoryId);
        return Result.success(page);
    }
    
    @Operation(summary = "获取图书详情")
    @GetMapping("/{id}")
    public Result<Book> getBook(@PathVariable Long id) {
        Book book = bookService.getById(id);
        return Result.success(book);
    }
    
    @Operation(summary = "获取热门图书")
    @GetMapping("/hot")
    public Result<List<Book>> getHotBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getHotBooks(limit);
        return Result.success(books);
    }
    
    @Operation(summary = "添加图书")
    @SaCheckRole("ADMIN")
    @PostMapping
    public Result<Void> addBook(@RequestBody Book book) {
        bookService.addBook(book);
        return Result.success("添加成功");
    }
    
    @Operation(summary = "更新图书")
    @SaCheckRole("ADMIN")
    @PutMapping("/{id}")
    public Result<Void> updateBook(@PathVariable Long id, @RequestBody Book book) {
        book.setId(id);
        bookService.updateBook(book);
        return Result.success("更新成功");
    }
    
    @Operation(summary = "删除图书")
    @SaCheckRole("ADMIN")
    @DeleteMapping("/{id}")
    public Result<Void> deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
        return Result.success("删除成功");
    }
}

